package com.common.base.widget

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.drawable.Drawable
import android.text.style.ImageSpan
import androidx.annotation.NonNull


/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：7/2/2023
 *
 * 描述：TextView图文混排,图片和文字居中对齐
 *
 * 修订历史：
 *
 */
class CenterAlignImageSpan : ImageSpan {
    constructor(drawable: Drawable?) : super(drawable!!) {}
    constructor(b: Bitmap?) : super(b!!) {}

    override fun draw(
        @NonNull canvas: Canvas,
        text: CharSequence?,
        start: Int,
        end: Int,
        x: Float,
        top: Int,
        y: Int,
        bottom: Int,
        @NonNull paint: Paint
    ) {
        val b = drawable
        val fm = paint.fontMetricsInt
        val transY = (y + fm.descent + y + fm.ascent) / 2 - b.bounds.bottom / 2 //计算y方向的位移
        canvas.save()
        canvas.translate(x, transY.toFloat()) //绘制图片位移一段距离
        b.draw(canvas)
        canvas.restore()
    }
}